/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dtn.wsn.sunspot;

import com.sun.spot.io.j2me.radiogram.RadiogramConnection;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.io.Connector; 

/**
 * spotconnection manager get and close the radio connection
 * and get and set the port number.
 * @author hao
 */
public class SunspotConnectionManager {

    private int DATA_SINK_PORT=67;
    
    private RadiogramConnection rCon = null;

    public SunspotConnectionManager() {
        try {
            
            this.rCon = (RadiogramConnection) Connector.open("radiogram://:" + DATA_SINK_PORT);
            
        } catch (IOException ex) {
            System.out.println("init sunspot connection failed!");
            Logger.getLogger(SunspotConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public SunspotConnectionManager(int port) {
        try {
            this.setDATA_SINK_PORT(port);
            this.rCon = (RadiogramConnection) Connector.open("radiogram://:" + DATA_SINK_PORT);
            
        } catch (IOException ex) {
            System.out.println("init sunspot connection failed!");
            Logger.getLogger(SunspotConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
   
    
    public RadiogramConnection getRadiogramConnection(){
        return rCon;
    }

   
    public void setDATA_SINK_PORT(int DATA_SINK_PORT) {
        this.DATA_SINK_PORT = DATA_SINK_PORT;
    }

    
    public int getDATA_SINK_PORT() {
        return DATA_SINK_PORT;
    }
    
    
    public void tearDownRadiogramConnection() throws Exception {
        if (rCon != null) {
            rCon.close();
        }
       
    }
}
